package org.jmeter.tcp.helmet;

import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.gui.util.FileDialoger;
import org.apache.jmeter.gui.util.FilePanel;
import org.apache.jmeter.gui.util.HorizontalPanel;
import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextArea;
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;
import org.jmeter.tcp.helmet.protobuf.util.Helmet2ServerMsgBuilder;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @author liangsu
 * @version v1.0
 * @Description
 * @Date 2020/11/13 15:46
 * @since
 */
public class HelmetGpsPushGui extends AbstractSamplerGui {

    JLabeledTextField address = new JLabeledTextField("address:");
    JLabeledTextField devId = new JLabeledTextField("devId:");
    FilePanel file = new FilePanel("gps file");

    private static final Logger log = LoggingManager.getLoggerForClass();

    public HelmetGpsPushGui(){
        init();
    }

    protected void init() {
        setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH); // Add the standard title

        JPanel mainPanel = new VerticalPanel();

        mainPanel.add(createMainPanel());

        add(mainPanel);
    }

    public JPanel createMainPanel(){
        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setBorder(makeBorder());

        HorizontalPanel line1 = new HorizontalPanel();
        line1.add(address);
        line1.add(devId);
        mainPanel.add(line1);

        HorizontalPanel line2 = new HorizontalPanel();
        line2.add(file);
        mainPanel.add(line2);

        return mainPanel;
    }


    @Override
    public String getLabelResource() {
        return this.getClass().getSimpleName();
    }

    /**
     * 静态的标签名称
     * @return
     */
    @Override
    public String getStaticLabel() {
        return "helmet gps push";
    }

    @Override
    public TestElement createTestElement() {
        HelmetGpsPushSampler sampler = new HelmetGpsPushSampler();
        modifyTestElement(sampler);
        return sampler;
    }

    /**
     * 将界面的数据保存到sampler
     * @param testElement
     */
    @Override
    public void modifyTestElement(TestElement testElement) {
        super.configureTestElement(testElement);

        HelmetGpsPushSampler sampler = (HelmetGpsPushSampler) testElement;
        sampler.setAddress(address.getText());
        sampler.setDevId(devId.getText());
        sampler.setFilePath(file.getFilename());

    }

    /**
     * 把Sampler中的数据加载到界面中
     * @param element
     */
    @Override
    public void configure(TestElement element) {
        super.configure(element);

        if(element instanceof HelmetGpsPushSampler){
            HelmetGpsPushSampler sampler = (HelmetGpsPushSampler) element;
            address.setText(sampler.getAddress());
            devId.setText(sampler.getDevId());
            file.setFilename(sampler.getFilePath());
        }

    }
}
